<? $canEdit = $user->hasCredential('halls.edit') ?>
<h1>
	Sala
	<span class="breadcrumbs">
		&lt; <a href="<?= $ro->gen('hall.browse') ?>">Sale</a>
	</span>
</h1>

<ul class="actions">
	<? if ($user->hasCredential('reservations.request')): ?><li><a href="<?= $ro->gen('reservation.request', array('hall' => $t['hall']['id'])) ?>">Rezerwuj salę</a></li><? endif ?>
	<? if ($canEdit): ?><li><a href="<?= $ro->gen('hall.edit', array('id' => $t['hall']['id'])) ?>">Edytuj salę</a></li><? endif ?>
	<? if ($user->hasCredential('halls.delete')): ?><li><a href="<?= $ro->gen('hall.delete', array('id' => $t['hall']['id'])) ?>">Usuń salę</a></li><? endif ?>
</ul>

<dl>
	<dt>ID</dt>
	<dd><?= $t['hall']['id'] ?></dd>
	<dt>Nazwa</dt>
	<dd><?= e($t['hall']['name']) ?></dd>
	<dt>Adres</dt>
	<dd><?= nl2br(e($t['hall']['address'])) ?></dd>
	<dt>Pojemność</dt>
	<dd><?= (int)$t['hall']['capacity'] ?> miejsc</dd>
  <dt>Galeria</dt>
  <dd>
    <ul id="gallery">
      <? foreach ($t['gallery'] as $item): ?>
      <li>
        <? if ($canEdit): ?><input type="checkbox" name="selectedImages[]" value="<?= $item['image'] ?>" /><? endif ?>
        <img src="<?= $item['thumb'] ?>" <?= $item['thumbSize'] ?> data-image="<?= $item['image'] ?>" data-width="<?= $item['width'] ?>" data-height="<?= $item['height'] ?>" />
      </li>
      <? endforeach ?>
    </ul>
		<? if ($canEdit): ?>
    <ul class="actions">
      <li><input id="deleteImage" type="button" value="Usuń zaznaczone obrazki" /></li>
      <li><input id="uploadImage" type="file" /></li>
    </ul>
		<? endif ?>
  </dd>
</dl>

<script type="text/javascript">
$(function()
{
	<? if ($canEdit): ?>
  var delImg = $('#deleteImage').click(function()
  {
    var imgs = [];
    
    $('input:checkbox:checked').each(function(_, el)
    {
      imgs.push(el.value);
    });

    if (imgs.length == 0) return;

    $.post('<?= $ro->gen('hall.deleteThumb', array('id' => $t['hall']['id'])) ?>', {images: imgs}, function(result)
    {
      if (result)
      {
        for (var i in imgs)
        {
          $('#gallery img[data-image="' + imgs[i] + '"]').parent().fadeOut(500, function() { $(this).remove(); });
        }
      }
    }, 'json');
  });

  $('#uploadImage').uploadify({
    uploader: '<?= AgaviConfig::get('app.path') ?>/vendor/uploadify/uploadify.swf',
    cancelImg: '<?= AgaviConfig::get('app.path') ?>/vendor/uploadify/cancel.png',
    script: '<?= $ro->gen('hall.uploadThumb', array('id' => $t['hall']['id'])) ?>',
    checkScript: '<?= $ro->gen('hall.checkThumb', array('id' => $t['hall']['id'])) ?>',
    auto: true,
    multi: true,
    simUploadLimit: 3,
    queueSizeLimit: 10,
    sizeLimit: 1000000,
    scriptAccess: 'always',
    fileDataName: 'image',
    buttonText: 'Dodaj obrazki',
    fileDesc: 'Pliki obrazków',
    fileExt: '*.jpg;*.png;*.gif;*.jpeg',
    onComplete: function(event, queueID, file, response, data)
    {
      var item = $.parseJSON(response);

      $('#gallery').append(
          '<li style="display: none;">'
          + '<input type="checkbox" name="selectedImages[]" value="' + item.image + '" />'
          + '<img src="' + item.thumb + '" ' + item.thumbSize + ' data-image="' + item.image + '" data-width="' + item.width + '" data-height="' + item.height + '" onclick="openZoom(this)" />'
        + '</li>');

      $('#gallery img[data-image="' + item.image + '"]').parent().fadeIn(500);
    }
  });
	<? endif ?>

  $('#gallery img').click(function()
  {
    openZoom(this);
  });
});
</script>